<?php

namespace app\zyhsmarketing\service;

use app\zyhsapi\model\marketing\ZyhsMarketingCallbackRecode;
use app\zyhsapi\model\ZyhsConfig;
use app\zyhsmarketing\lib\HuaweiMarketing;
use app\zyhsmarketing\lib\OppoMarketing;
use app\zyhsmarketing\lib\VivoMarketing;
use app\zyhsmarketing\lib\XmMarketing;

class MarketingCallback
{
    /**
     * 上传转化数据
     * @param string $oaid 设备ID
     * @param int $uid 用户ID
     * @param int $targetType 转化指标 1=>激活 2=>注册 3=>充值
     * @param int $mobileType 华为=>5 小米=>4 vivo=>3 oppo=>2
     * @return void
     */
    public function uploadActiveData(string $oaid, int $uid, int $targetType, int $mobileType = 0)
    {
        if (empty($oaid) || !in_array($mobileType, [5, 4, 3, 2], true))
        {
            return;
        }
        // 判断 是否 开启回传
        $config = (new ZyhsConfig())->configInfo('marketing_callback');
        if (!$config)
        {
            return;
        }
        $zyhsMarketingCallbackRecodeModel = new ZyhsMarketingCallbackRecode();
        $isCallback = $zyhsMarketingCallbackRecodeModel->where(['oaid' => $oaid, 'type' => $mobileType, 'target_type' => $targetType])->count();
        if ($isCallback)
        {   // 已经回传过了
            return;
        }
        // 进行 回传
        switch ($mobileType)
        {
            case 5: // 华为
                $huaweiSwitch = (int)($config['huawei_switch'] ?? 0);
                if ($huaweiSwitch !== 1)
                {   // 回传 未开启 停止回传
                    return;
                }
                (new HuaweiMarketing())->callBack($oaid, $uid, $targetType);
                break;
            case 4: // 小米
                $xiaomiSwitch = (int)($config['xiaomi_switch'] ?? 0);
                if ($xiaomiSwitch !== 1)
                {   // 回传 未开启 停止回传
                    return;
                }
                (new XmMarketing())->callBack($oaid, $uid, $targetType);
                break;
            case 3: // VIVO
                $vivoSwitch = (int)($config['vivo_switch'] ?? 0);
                if ($vivoSwitch !== 1)
                {   // 回传 未开启 停止回传
                    return;
                }
                (new VivoMarketing())->callBack($oaid, $uid, $targetType);
                break;
            case 2: // OPPO
                $oppoSwitch = (int)($config['oppo_switch'] ?? 0);
                if ($oppoSwitch !== 1)
                {   // 回传 未开启 停止回传
                    return;
                }
                (new OppoMarketing())->callBack($oaid, $uid, $targetType);
                break;
            default:
        }
    }
}
